//
//  ViewController.swift
//  C017_Animation
//
//  Created by yao_yu on 15/6/22.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var aView:UIView!
    var userName: UITextField!
    var password: UITextField!
    var animationContainerView: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupUI3()
    }
    
    func setupUI3() {
        animationContainerView = UIView(frame: view.bounds)
        animationContainerView.frame = view.bounds
        view.addSubview(animationContainerView!)
    }
    
    func setupUI(){
        //let WIDTH = view.frame.width
        userName = UITextField(frame: CGRectMake(100, 40, 300, 30))
        userName.center.x = view.frame.width / 2 - view.frame.width
        userName.placeholder = "用户名"
        view.addSubview(userName)
        password = UITextField(frame: CGRectMake(100, 100, 300, 30))
        password.center.x = view.frame.width / 2 - view.frame.width
        password.placeholder = "密码"
        view.addSubview(password)
        
//        UIView.animateWithDuration(0.5) { () -> Void in
//            self.userName.center.x += WIDTH
//            self.password.center.x += WIDTH
//        }
    }
    
    override func viewDidAppear(animated: Bool) {
        viewDidAppear3(animated)
    }
    
    func viewDidAppear3(animated: Bool) {
        //create new view
        let newView = UIImageView(image: UIImage(named: "banner.jpg")!)
        newView.center = animationContainerView!.center
        //add the new view via transition
        UIView.transitionWithView(animationContainerView, duration: 3.33,
            options: UIViewAnimationOptions.TransitionFlipFromBottom, animations: {
            self.animationContainerView!.addSubview(newView)
            
            
            }, completion: {[unowned self] (res) -> Void in
            //remove the view via transition
            UIView.transitionWithView(self.animationContainerView, duration: 5.33,
                options:  UIViewAnimationOptions.TransitionCrossDissolve, animations: {
                
                newView.removeFromSuperview() }, completion: nil)

            })

    }
    
    func viewDidAppear2(animated: Bool) {
        UIView.animateWithDuration(1.5, delay: 0.4, options:  UIViewAnimationOptions.CurveEaseOut, animations: {[unowned self] () -> Void in
            self.userName.center.x += self.view.frame.width
            self.password.center.x +=  self.view.frame.width
            }, completion: nil)
    }
    
    func setupUI2(){
        aView = UIView(frame: CGRectMake(100, 100, 100, 100))
        aView.backgroundColor = UIColor.redColor()
        view.addSubview(aView)
        
        aView.transform = CGAffineTransformMakeTranslation(300, 300)
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: -0.5, options: UIViewAnimationOptions.AllowUserInteraction, animations: {[unowned self] () -> Void in
            self.aView.transform = CGAffineTransformIdentity
            }, completion: nil)
        
        var aButton:UIButton = UIButton(type: UIButtonType.System)
        aButton.frame = CGRectMake(20, 20, 100, 30)
        aButton.setTitle("动画1", forState: UIControlState.Normal)
        aButton.addTarget(self, action: "moveAnimate1", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(aButton)
        
        aButton = UIButton(type: UIButtonType.System)
        aButton.frame = CGRectMake(100, 20, 100, 30)
        aButton.setTitle("动画2", forState: UIControlState.Normal)
        aButton.addTarget(self, action: "moveAnimate2", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(aButton)
        
        aButton = UIButton(type: UIButtonType.System)
        aButton.frame = CGRectMake(180, 20, 100, 30)
        aButton.setTitle("复原", forState: UIControlState.Normal)
        aButton.addTarget(self, action: "moveAnimate3", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(aButton)
    }
    
    func moveAnimate1() {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: -0.5, options: UIViewAnimationOptions.AllowUserInteraction, animations: {[unowned self] () -> Void in
            self.aView.transform = CGAffineTransformMakeTranslation(10, 10)
            }, completion: nil)
    }
    
    func moveAnimate2() {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: -0.5, options: UIViewAnimationOptions.AllowUserInteraction, animations: {[unowned self] () -> Void in
            self.aView.transform = CGAffineTransformTranslate(self.aView.transform, 10, 10)
            }, completion: nil)
    }
    
    func moveAnimate3() {
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: -0.5, options: UIViewAnimationOptions.AllowUserInteraction, animations: {[unowned self] () -> Void in
            self.aView.transform = CGAffineTransformIdentity
            }, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

